﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Collections.Specialized;
using LightWeb.DomainModel;

namespace LightWeb.Application.WebSecurity
{
    public class NHRoleProvider : RoleProvider
    {
        NameValueCollection Config { get; set; }

        IRoleProviderRepository Repository { get; set; }

        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            base.Initialize(name, config);
            Repository = new SecurityService();
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            Repository.AddUsersToRoles(usernames, roleNames);
        }

        string _AppName = string.Empty;
        public override string ApplicationName
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_AppName))
                    _AppName = Config["ApplicationName"];
                return _AppName;
            }
            set { _AppName = value; }
        }

        public override void CreateRole(string roleName)
        {
            Repository.CreateRole(roleName);
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            return Repository.DeleteRole(roleName, throwOnPopulatedRole);
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            var usernames = new List<string>();

            IList<Role> roles = Repository.GetUsersInRole(roleName, usernameToMatch);

            foreach (var item in roles)
            {
                usernames.Add(item.Name);
            }
            return usernames.ToArray();
        }

        public override string[] GetAllRoles()
        {
            var roles = Repository.GetAllRoles();
            var rolesname = new List<string>();
            foreach (var item in roles)
            {
                rolesname.Add(item.Name);
            }
            return rolesname.ToArray();
        }

        public override string[] GetRolesForUser(string username)
        {
            List<string> rolenames = new List<string>();

            IList<Role> roles = Repository.GetRolesForUser(username);
            foreach (var item in roles)
            {
                rolenames.Add(item.Name);
            }
            return rolenames.ToArray();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            List<string> usernames = new List<string>();
            IList<User> users = Repository.GetUsersInRole(roleName);
            foreach (var item in users)
            {
                usernames.Add(item.LoginName);
            }

            return usernames.ToArray();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            return Repository.IsUserInRole(username, roleName);
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            Repository.RemoveUsersFromRoles(usernames, roleNames);
        }

        public override bool RoleExists(string roleName)
        {
            return Repository.RoleExists(roleName);
        }
    }
}
